angular.module('imsApp').controller('focusController', ['$scope', '$rootScope', '$state', 'FocusManager', '$filter', 'DisallowManager','ImsDialog', function ($scope, $rootScope, $state, FocusManager, $filter, DisallowManager,ImsDialog) {
    $scope.search = {
        pageNo: $state.params.pageNo || 1,
        pageSize: $state.params.pageSize || 10,
        type: $state.params.type || "",
        mode: $state.params.mode || 'cuid',
        startDay: $state.params.startDay || null,
        endDay: $state.params.endDay || null
    };

    $scope.types = $scope.search.mode == 'ip' ? [{type:null,value:'全部'},{type: 401, value: $scope.table[401]}, {
        type: 402,
        value: $scope.table[402]
    }] : [{type:null,value:'全部'},{type: 403, value: $scope.table[403]}, {type: 404, value: $scope.table[404]}]
    $scope.typeObj = $scope.search.type ? $filter("filter")($scope.types, {type: $scope.search.type})[0] : null;
    $scope.statusArray = [{key: 500, value: $scope.table[500]}, {key: 501, value: $scope.table[501]}];
    $scope.typeChange = function (typeObj) {
        if (typeObj.type) {
            $scope.search.type = typeObj.type;

        } else {
            $scope.search.type = null;

        }
        console.log(typeObj);
        console.log($scope.search);
        $state.go($state.current.name, $scope.search)

    }

    $scope.rangeDate = {
        startDate:$scope.search.startDay ? moment($scope.search.startDay).format('X'):null,
        endDate:$scope.search.endDay? moment($scope.search.endDay).format('X'):null,
        onSelected: function (data, scope) {  //时间选择确认事件
            $scope.search.startDay =data.startDate? moment(new Date(parseFloat(data.startDate) * 1000)).format("YYYY-MM-DD"):null;
            $scope.search.endDay = data.endDate?moment(new Date(new Date(parseFloat(data.endDate) * 1000))).format("YYYY-MM-DD"):null;
            $state.go($state.current.name, $scope.search)

        }
    };
    $scope.paginationConf = {
        currentPage: $state.params.pageNo || 1,
        itemsPerPage: $state.params.pageSize || 10,
        pagesLength: 9,
        search: 0,
        onChange: function () {
            $scope.search.pageNo = this.currentPage;
            $scope.search.pageSize = this.itemsPerPage;
            $state.go($state.current.name, $scope.search)
        }
    };

    $scope.filterClick = function (mode) {
        $scope.search.mode = mode;
        $scope.search.type = null;
        $scope.search.pageNo = 1;
        $scope.search.pageSize = 10;
        $state.go($state.current.name, $scope.search)

    }


    if ($scope.search.mode != "ip") {
        FocusManager.cuidList([$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
            if (result.data.c == 's') {
                $scope.list = result.data.pageList;
                $scope.paginationConf.totalItems = result.data.total;
            }
        })

    } else {
        FocusManager.ipList([$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
            if (result.data.c == 's') {
                $scope.list = result.data.pageList;
                $scope.paginationConf.totalItems = result.data.total;
            }
        })
    }

    $scope.allCheckedClick = function (checked) {
        $scope.list.forEach(function (item) {
            item.checked = checked
        })
    }

    /**
     *  封禁
     * @param item
     */
    $scope.disAllowed = function (item) {
        if ($scope.search.mode == "ip") {
            DisallowManager.ipAdd({list: [[item.ip, item.type]]}).then(function (result) {
                if (result.data.c == "s") {
                    $state.reload();
                }
            })
        } else {
            DisallowManager.cuidAdd({list: [[item.cuid, item.type]]}).then(function (result) {
                if (result.data.c == "s") {
                    $state.reload();
                }
            })
        }
    }

    /**
     *  封禁选择的所有
     * @param item
     */
    $scope.disAllowedSelect = function () {
        var tmpArray=[]
        $scope.list.forEach(function (item) {
            if (item.checked) {
                tmpArray.push([item.cuid?item.cuid:item.ip,item.type]);
            }
        })
        if ($scope.search.mode == "ip") {
            DisallowManager.ipAdd({list: tmpArray}).then(function (result) {
                if (result.data.c == "s") {
                    $state.reload();
                }
            })
        } else {
            DisallowManager.cuidAdd({list: tmpArray}).then(function (result) {
                if (result.data.c == "s") {
                    $state.reload();
                }
            })
        }
    }


    var success = function (result) {
        if (result.data.c == "s") {
            if (result.data.c == 's') {
                $state.reload();
            }
        }
    }

    $scope.unFocusSelect = function () {
        var ids = [];
        $scope.list.forEach(function (item) {
            if (item.checked) {
                ids.push(item.id)
            }
        })
        if ($scope.search.mode != "ip") {
            FocusManager.cuIdUnFocus(ids).then(function (result) {
                success(result);
            })
        } else {
            FocusManager.ipUnFocus(ids).then(function (result) {
                success(result);
            })
        }
    }


    $scope.unFocus = function (id) {
        if ($scope.search.mode != "ip") {
            FocusManager.cuIdUnFocus([id]).then(function (result) {
                success(result);
            })
        } else {
            FocusManager.ipUnFocus([id]).then(function (result) {
                success(result);
            })
        }

    }


    $scope.deletes = function (key) {
        $scope.search[key] = null;
    }

    $scope.changeStatus = function (item) {
        var status = "";
        if (item.status == 3) {
            status = 4;
        } else {
            status = 3;
        }
        CampaignsManager.campaignsStatusEdit(item.campaignId, {status: status}).then(function (result) {
            $state.reload();
        })
    }


}])

